package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IServiceItemService serviceItemService;

    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/itemDetail")
    public String listPage(Model model,Long statementId){
        //通过传过来的statementId,查询结算单
        Statement statement= statementService.get(statementId);
        model.addAttribute("statement",statement);
        //
        //如果结算单的状态为消费中,则跳转到明细编辑页面itemAdd.html
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "itemAdd";
        }else {
            Long payeeId = statement.getPayeeId();
            User payee = userService.get(payeeId);
            statement.setPayee(payee);
            return prefix + "itemDetail";
        }
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:query")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.selectByStatementId(qo);
    }

    //@RequestBody注解: 通过Ajax请求发送JSON数据,可以吧JSON数据封装对象中
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items){
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        statementService.payStatement(statementId);
        return AjaxResult.success();
    }
}
